{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8388f063",
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import date\n",
    "from gs_quant.common import PayReceive, Currency\n",
    "from gs_quant.instrument import CDIndex\n",
    "from gs_quant.session import Environment, GsSession\n",
    "from gs_quant.markets import HistoricalPricingContext, PricingContext\n",
    "from gs_quant.risk import CDATMSpread"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f7993feb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# external users should substitute their client id and secret; please skip this step if using internal jupyterhub\n",
    "GsSession.use(Environment.PROD, client_id=None, client_secret=None, scopes=('run_analytics'))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34576b63",
   "metadata": {},
   "source": [
    "#### Available index families for CDIndex\n",
    "- iTraxx Europe OTR: 3y, 5y, 7y, 10y\n",
    "- iTraxx Europe XOVER OTR: 5y\n",
    "- CDX.NA.IG OTR: 5y\n",
    "- CDX.NA.HY OTR: 3y, 5y, 7y, 10y\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1c092ad7",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# Create an index product on S34 main\n",
    "itraxx_main = CDIndex(index_family='iTraxx Europe', termination_date='5y', index_series=32)\n",
    "\n",
    "with PricingContext(date(2021, 8, 10)):\n",
    "    itraxx_main.resolve()\n",
    "itraxx_main.as_dict()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "76106f9b",
   "metadata": {},
   "source": [
    "Also create an index product on iTraxx Xover and price historically alongside iTraxx Main.\n",
    "\n",
    "Instead of pricing these, we will calculate the ATM spread"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "22776a0d",
   "metadata": {},
   "outputs": [],
   "source": [
    "itraxx_xover = CDIndex(index_family='iTraxx Europe XOVER', index_series=32)\n",
    "\n",
    "with HistoricalPricingContext(date(2020, 3, 9), date(2020, 3, 27), show_progress=True, is_batch=True):\n",
    "    itraxx_main_res_f = itraxx_main.calc(CDATMSpread)\n",
    "    itraxx_xover_res_f = itraxx_xover.calc(CDATMSpread)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ac6ff3fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "itraxx_main_res_f.result()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d761f1b6",
   "metadata": {},
   "outputs": [],
   "source": [
    "itraxx_main_res = itraxx_main_res_f.result()\n",
    "itraxx_xover_res = itraxx_xover_res_f.result()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ebae46a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "fig, ax = plt.subplots()\n",
    "ax.plot(itraxx_main_res, label=\"iTraxx Main\")\n",
    "ax.plot(itraxx_xover_res, label=\"iTraxx XOVER\" )\n",
    "ax.set_xlabel('time')\n",
    "ax.set_ylabel('ATM Spread ($)')\n",
    "ax.set_title('ATM Spread over Time')\n",
    "ax.legend()\n",
    "fig.autofmt_xdate()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
